package com.example.homework;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.homework.dao.LoginDao;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    ImageButton register_switch, wenjuanxing_login;
    EditText username, password;
    CheckBox agreement, remember_password, auto_login;
    TextView user_agreement, user_privacy;
    SharedPreferences sharedPreferences;

    public static MainActivity instance = null;
    LoginDao loginDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        register_switch = findViewById(R.id.register_switch);
        wenjuanxing_login = findViewById(R.id.wenjuanxing_login);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        remember_password = findViewById(R.id.remember_password);
        auto_login = findViewById(R.id.auto_login);
        agreement = findViewById(R.id.agreement);
        user_agreement = findViewById(R.id.user_agreement);
        user_agreement.setMovementMethod(LinkMovementMethod.getInstance());
        user_privacy = findViewById(R.id.user_privacy);
        user_privacy.setMovementMethod(LinkMovementMethod.getInstance());

        instance = this;

        loginDao = new LoginDao(this);


        /**
         * 使用共享首选项
         * 用于记住密码
         * 和自动登录
         *
         */
        //文件名
        sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        //创建字段
        String share_uname = sharedPreferences.getString("username", null);
        String share_upwd = sharedPreferences.getString("password", null);
        String is_remember_pwd = sharedPreferences.getString("is_remember_password", null);
        String is_auto_login = sharedPreferences.getString("is_auto_login", null);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        //判断用户上一次是否选中记住密码
        if (Objects.equals(is_remember_pwd, "true")) {
            remember_password.setChecked(true);
            username.setText(share_uname);
            password.setText(share_upwd);
        }
        if (Objects.equals(is_auto_login, "true")) {
            Intent intent = new Intent(getApplicationContext(), BottomNavigationActivity.class);
            intent.putExtra("uname", username.getText().toString());
            startActivity(intent);
            Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
        }


        /**
         * 这是注册按钮的点击事件
         * 点击跳转到用户注册页面
         */
        register_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
            }
        });


        /**
         * 登录按钮的点击事件
         * 点击若登陆成功则跳转至主菜单界面
         * 进行登录时用户名以及密码的验证
         */
        wenjuanxing_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (agreement.isChecked()) {
                    if (username.length() != 0 && password.length() != 0) {
                        boolean loginValidate = loginDao.loginValidate(username.getText().toString(), password.getText().toString());
                        if (loginValidate) {
                            /**
                             * 登陆成功后，将账号密码写入共享首选项
                             * 以及用户是否勾选记住密码
                             *
                             */
                            if (remember_password.isChecked()) {
                                editor.putString("is_remember_password", "true");
                            } else {
                                editor.putString("is_remember_password", "false");
                            }
                            if (auto_login.isChecked()) {
                                editor.putString("is_auto_login", "true");
                            } else {
                                editor.putString("is_auto_login", "false");
                            }
                            editor.putString("username", username.getText().toString());
                            editor.putString("password", password.getText().toString());
                            editor.commit();


                            Intent intent = new Intent(getApplicationContext(), BottomNavigationActivity.class);
                            intent.putExtra("uname", username.getText().toString());
                            startActivity(intent);
                            Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            username.setText("");
                            password.setText("");
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "请先阅读并同意用户协议", Toast.LENGTH_SHORT).show();
                }
                //方便测试，免登录
//                Intent intent = new Intent(getApplicationContext(), BottomNavigationActivity.class);
//                startActivity(intent);
//                Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}